package com.pengheng.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.pengheng.vo.PayVo;
import com.pengheng.vo.QueryRefoundVo;
import com.pengheng.vo.RefoundVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @remark
 * @Author pengheng
 * @date 2021/5/19 9:43
 */
@Component
@ConfigurationProperties(prefix = "alipay")
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    private String appId;

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchantPrivateKey;

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipayPublicKey;

    // 服务器异步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    private String notifyUrl;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    private String returnUrl;

    // 签名方式
    private String signType;

    // 序列化类型
    private String format;

    // 字符编码格式
    private String charset;

    // 支付宝网关
    private String gatewayUrl;

    @PostConstruct
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(gatewayUrl, appId, merchantPrivateKey, format, charset, alipayPublicKey, signType);
    }

    public String pay(PayVo payVo) throws AlipayApiException {
        AlipayClient alipayClient = alipayClient();

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + payVo.getOut_trade_no() + "\","
                + "\"total_amount\":\"" + payVo.getTotal_amount() + "\","
                + "\"subject\":\"" + payVo.getSubject() + "\","
                + "\"body\":\"" + payVo.getBody() + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        return result;
    }

    public String queryPay(String out_trade_no, String trade_no) throws AlipayApiException {
        AlipayClient alipayClient = alipayClient();
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"}");
        String result = alipayClient.execute(alipayRequest).getBody();
        return result;
    }

    public String refound(RefoundVo refoundVo) throws AlipayApiException {
        AlipayClient alipayClient = alipayClient();
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + refoundVo.getOut_request_no() + "\","
                + "\"trade_no\":\"" + refoundVo.getTrade_no() + "\","
                + "\"refund_amount\":\"" + refoundVo.getRefund_amount() + "\","
                + "\"refund_reason\":\"" + refoundVo.getRefund_reason() + "\","
                + "\"out_request_no\":\"" + refoundVo.getOut_request_no() + "\"}");
        String result = alipayClient.execute(alipayRequest).getBody();
        return result;
    }

    public String queryRefound(QueryRefoundVo queryRefoundVo) throws AlipayApiException {
        AlipayClient alipayClient = alipayClient();
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + queryRefoundVo.getOut_trade_no() + "\","
                + "\"trade_no\":\"" + queryRefoundVo.getTrade_no() + "\","
                + "\"out_request_no\":\"" + queryRefoundVo.getOut_request_no() + "\"}");
        return alipayClient.execute(alipayRequest).getBody();
    }

    public String closeOrder(String out_trade_no, String trade_no) throws AlipayApiException {
        AlipayClient alipayClient = alipayClient();
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"}");
        return alipayClient.execute(alipayRequest).getBody();
    }
}
